#include "delay.h"

uint8_t fus = 0;
uint16_t fms = 0;

void delay_init(uint8_t clkMhz)
{
	fus = clkMhz / 8;
	fms = fus * 1000;
	SysTick->CTRL &= ~(1 << 2);
}
	
//max798ms
void delay_ms(uint16_t ms)
{
	SysTick->LOAD = fms * ms;
	SysTick->VAL = 0;
	SysTick->CTRL |= 0x01;
  while( !(SysTick->CTRL & (1<<16)) );
	
	SysTick->CTRL = 0x00;
}

void delayMs(uint16_t ms) 
{
	uint16_t i = ms / 790;
	uint16_t r = ms % 790;
	while(i)
	{
		delay_ms(790);
		i--;
	}
	delay_ms(r);
}
	
void delay_us(uint32_t us)
{
	SysTick->LOAD = fus * us;
	SysTick->VAL = 0;
	SysTick->CTRL |= 0x01;
  while( !(SysTick->CTRL & (1<<16)) );
	
	SysTick->CTRL = 0x00;
}